package nhatquang.gs.main;

import nhatquang.gs.controler.ResourcesManager;
import nhatquang.gs.controler.ScenesManager;
import nhatquang.gs.model.Public;
import nhatquang.gs.model.UserData;

import org.andengine.engine.Engine;
import org.andengine.engine.FixedStepEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.BaseGameActivity;

import android.view.KeyEvent;

public class MainActivity extends BaseGameActivity {
	protected Camera mCamera;
	protected EngineOptions mEngineOptions;
	protected UserData userData;

	@Override
	public EngineOptions onCreateEngineOptions() {
		// DisplayMetrics mDisplayMertric = new DisplayMetrics();
		// getWindowManager().getDefaultDisplay().getMetrics(mDisplayMertric);
		// Public.WIDTH_SCREEN = mDisplayMertric.widthPixels;
		// Public.HEIGHT_SCREEN = mDisplayMertric.heightPixels;
		Public.activity = this;
		mCamera = new Camera(0, 0, Public.WIDTH_SCREEN, Public.HEIGHT_SCREEN);
		mEngineOptions = new EngineOptions(true,
				ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(),
				mCamera);
		mEngineOptions.getAudioOptions().setNeedsMusic(true);
		mEngineOptions.getAudioOptions().setNeedsSound(true);
		mEngineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
		userData = new UserData();
		userData.init(this);

		return mEngineOptions;
	}

	@Override
	public void onCreateResources(
			OnCreateResourcesCallback pOnCreateResourcesCallback)
			throws Exception {
		ResourcesManager.prepareManager(mEngine, this, mCamera,
				getVertexBufferObjectManager(), userData);
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
			throws Exception {
		ScenesManager.getInstance().createSplashScene(pOnCreateSceneCallback);
	}

	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
		return new FixedStepEngine(pEngineOptions, 60);
	}

	@Override
	public void onPopulateScene(Scene pScene,
			OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		mEngine.registerUpdateHandler(new TimerHandler(2f,
				new ITimerCallback() {

					@Override
					public void onTimePassed(TimerHandler pTimerHandler) {
						ResourcesManager.INSTANCE.loadResource();
					}
				}));
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	// @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ScenesManager.getInstance().getCurrentScene().onBackKeyPressed();
		}
		if (keyCode == KeyEvent.KEYCODE_HOME) {
			// ------------------do some thing --------------
		}
		return false;
	}
}
